<?php

class UserController extends FrontEndController {

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex() {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('index');
    }

    /**
     * Displays the login page
     */
    public function actionLogin() {
        if (Yii::app()->user->id !== null) {
            $this->redirect(Yii::app()->homeUrl);
        } else {
            $model = new LoginForm;

            // if it is ajax validation request
            if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            // collect user input data
            if (isset($_POST['LoginForm'])) {
                $model->attributes = $_POST['LoginForm'];
                // validate user input and redirect to the previous page if valid
                if ($model->validate() && $model->login())
                    $this->redirect(Yii::app()->user->returnUrl);
            }
            // display the login form
            $this->render('login', array('model' => $model));
        }
    }

    public function actionRegister() {
        if (Yii::app()->user->id !== null) {
            $this->redirect(Yii::app()->homeUrl);
        } else {
            $model = new User();
            // if it is ajax validation request
            if (isset($_POST['ajax']) && $_POST['ajax'] === 'register-form') {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }
            if (isset($_POST['User'])) {
                $model->attributes = $_POST['User'];
                $model->created_at = date('Y-m-d H:i:s');
                $model->updated_at = date('Y-m-d H:i:s');
                $pass_login = $model->password;
                $model->password = md5($model->password);
                if ($model->save()) {
                    $identity = new UserIdentity($model->email, $pass_login);
                    $identity->authenticate();
                    $duration = 3600 * 24 * 30; // 30 days
                    Yii::app()->user->login($identity, $duration);
                    $this->redirect(Yii::app()->user->returnUrl);
                }
            }
            $this->render('register', array('model' => $model));
        }
    }

    public function actionProfile() {
        if (Yii::app()->user->id == null) {
            $this->redirect(Yii::app()->createUrl('site/login'));
        } else {
            $model = User::model()->findByPk(Yii::app()->user->id);
            if (isset($_POST['User'])) {
                $model->attributes = $_POST['User'];
                $model->updated_at = date('Y-m-d H:i:s');
                if ($model->save()) {
                    Yii::app()->user->setFlash('success', 'Cập nhật thông tin thành công.');
                    $this->refresh();
                } else {
                    Yii::app()->user->setFlash('error', 'Đã có lỗi xảy ra. Vui lòng kiểm tra lại.');
                }
            }
            $this->render('profile', array('model' => $model));
        }
    }

    public function actionChangePassword() {
        if (Yii::app()->user->id == null) {
            $this->redirect(Yii::app()->createUrl('site/login'));
        } else {
            $model = new ChangePasswordForm();
            if (isset($_POST['ChangePasswordForm'])) {
                $model->attributes = $_POST['ChangePasswordForm'];
                if ($model->validate() && $model->changePassword()) {
                    Yii::app()->user->setFlash('success', 'Thay đổi mật khẩu thành công.');
                    $this->redirect(array('user/profile'));
                } else {
                    Yii::app()->user->setFlash('error', 'Đã có lỗi xảy ra. Vui lòng kiểm tra lại.');
                }
            }
            $this->render('change_password', array('model' => $model));
        }
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

    public function actionListNews() {
        $thongBaoMoiThau = Form::model()->findAll('type=5 and user_create=' . Yii::app()->user->id);
        $keHoachDauThau = Form::model()->findAll('type=4 and user_create=' . Yii::app()->user->id);
        $this->render('list_news', array('thongBaoMoiThau' => $thongBaoMoiThau, 'keHoachDauThau' => $keHoachDauThau));
    }

}
